<?php

namespace Store\Model;

//use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
//use Zend\XmlRpc\Value\String;

use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class Store_products_Table 
{
	protected $tableGateway;
	
	public function __construct(TableGateway $tableGateway)
	{
		$this->tableGateway = $tableGateway;
		//$this->uploadSharingTableGateway = $uploadSharingTableGateway;
	}
	public function saveStore_products(Store_products $Store_product)
	{
		$data = array(
				'name' => $Store_product->name,
				'desc' => $Store_product->desc,
				'cost' => $Store_product->cost,
			
		);
		$id = (int)$Store_product->id;
		if ($id == 0) {
			$this->tableGateway->insert($data);
		} else {
			if ($this->getUpload($id)) {
				$this->tableGateway->update($data, array('id' => $id));
			} else {
				throw new \Exception('Product ID does not exist');
			}
		}
	}
	
	public function getStore_products($id)
	{
		$id = (int) $id;
		$rowset = $this->tableGateway->select(array('id' => $id));
		$row = $rowset->current();
		if (!$row) {
			throw new \Exception("Could not find row $id");
		}
		return $row;
	}
	
	/*public function getlastStore_products()
	{
		//$rowset = array();
		$rowset = $this->tableGateway->getLastInsertValue();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('  '.var_dump($rowset));
		return $rowset;
	}
	
	public function get($getStore_products.....)
	{
		$name = (string)$filename;
		//throw new \Exception("Could not find row $name");
		$rowset = $this->tableGateway->select(array('filename' => $name));
		$row = $rowset->current();
		//throw new \Exception("Could not find row $rowset");
		if (!$row) {
			//throw new \Exception("Could not find row $row");
			return NULL;
		}
		return $row;
	}
	
	public function fetchAll($paginated=false)
	{
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}*/
	
	public function fetchAll($paginated=false)
	{
		
		if ($paginated) {
			// create a new Select object for the table products
			$select = new Select('store_products');
			// create a new result set based on the product entity
			$resultSetPrototype = new ResultSet();
			$resultSetPrototype->setArrayObjectPrototype(new Store_products());
			// create a new pagination adapter object
			$paginatorAdapter = new DbSelect(
					// our configured select object
					$select,
					// the adapter to run it against
					$this->tableGateway->getAdapter(),
					// the result set to hydrate
					$resultSetPrototype
			);
			$paginator = new Paginator($paginatorAdapter);
			//throw new \Exception("$paginator");
			return $paginator;
		}
		$resultSet = $this->tableGateway->select();
		return $resultSet;
	}
	
	
	public function deleteStore_products($id)
	{
		$this->tableGateway->delete(array('id' => $id));
	}
	
	
	
}

?>
